import java.io.*;

import org.apache.commons.lang.StringUtils;

import java.util.Dictionary;
import java.util.Hashtable;

class SpaceToEarthConverter {

    Dictionary<Character, Integer> space2roman;
    Dictionary<String, Character> aliasnames;
    Dictionary<String, Double> coincredit;

    SpaceToEarthConverter() {
        space2roman = new Hashtable<Character, Integer>();
        space2roman.put('I', 1);
        space2roman.put('V', 5);
        space2roman.put('X', 10);
        space2roman.put('L', 50);
        space2roman.put('C', 100);
        space2roman.put('D', 500);
        space2roman.put('M', 1000);
        aliasnames = new Hashtable<String, Character>();
        coincredit = new Hashtable<String, Double>();
    }

    public static void main(String args[]) throws Exception {
        String source;
        BufferedReader br;
        SpaceToEarthConverter e = new SpaceToEarthConverter();
        String breakstring[] = null;
        while (true) {
            System.out.print(">>");
            br = new BufferedReader(new InputStreamReader(System.in));
            source = br.readLine();
            if (source.equals("exit")) {
                System.exit(0);
            }
            breakstring = source.split(" ");
            if (breakstring.length == 3)
                e.choice1(source);
            else if (breakstring[breakstring.length - 1].equals("Credits"))
                e.choice2(source);
            else if (breakstring[breakstring.length - 1].equals("?"))
                e.choice3(source);
            else
                System.out.println("       I don't know what you're talking about");
        }
    }
    void choice1(String source) throws Exception {

        String sentence_broken[] = source.split(" ");
        if (sentence_broken.length != 3)
            System.out.println("Something is not right!");
        else {
            if (sentence_broken[1].equals("is")) {
                if (sentence_broken[2].length() == 1) {
                    char t = sentence_broken[2].charAt(0);
                    if (space2roman.keys() == null) {
                        System.out.println("I don't understand what you are saying");
                    } else {
                        add_alias(sentence_broken[0], t);
                        System.out.println(sentence_broken[0] + "<---->" + t);
                    }
                } else
                    System.out.println("I don't understand what you are saying");
            } else
                System.out.println("I don't understand what you are saying.");
        }
    }

    void choice2(String source) throws Exception {

        int i, index = -1;
        int cred_count;
        String coin_value;
        String sentence_broken[] = source.split(" ");
        for (i = 0; i < sentence_broken.length; i++) {
            if (sentence_broken[i].equals("is")) {
                index = i;
                break;
            }
        }
        if (index == -1)
            System.out.println("I don't understand what you're saying");
        else {
            //Now check if the last word is "Credits"
            //So if index is where the "is" is, then index+2 should have the word "Credits"
            if (sentence_broken[i + 2].equals("Credits")) {
                //Proceed to break up the sentence
                if (StringUtils.isNumeric(sentence_broken[i + 1])) {
                    cred_count = Integer.parseInt(sentence_broken[i + 1]);
                    //coin_value=sentence_broken[i-1];
                    coin_value = sentence_broken[0];
                    for (int j = 1; j <= i - 2; j++)
                        coin_value = coin_value + " " + sentence_broken[j];
                    System.out.println(coin_value);
                    assign_credits(coin_value, sentence_broken[i - 1], cred_count);
                }
            } else
                System.out.println("I don't understand what you're saying");

        }

    }

    void choice3(String source) throws Exception {
        String sentence_broken[] = source.split(" ");
        // Parse the line
        //1. Is the first word "How"
        //2. Is the second word "much" or "many"
        if (sentence_broken[0].equals("how")) {
            if (sentence_broken[1].equals("much")) {
                if (sentence_broken[2].equals("is")) {
                    String check = new String();
                    check = sentence_broken[3];
                    for (int i = 4; i < sentence_broken.length - 1; i++)
                        check = check + " " + sentence_broken[i];
                    System.out.println(check);
                    int value = break_string(check);

                    System.out.println(value);
                    // Use the function call like this : break_string("pish tegj glob glob");
                }
            } else if (sentence_broken[1].equals("many")) {
                if (sentence_broken[2].equals("Credits") && sentence_broken[3].equals("is")) {

                    int last = sentence_broken.length - 1;
                    if (sentence_broken[last].equals("?")) {
                        last = last - 1;
                    }
                    String coin = sentence_broken[last];
                    String value;
                    value = sentence_broken[4];
                    for (int i = 5; i < last; i++) {
                        value = value + " " + sentence_broken[i];
                    }
                    System.out.println(value + " " + coin);
                    double t = calc_credit(value, coin);
                    System.out.println(t);
                } else
                    System.out.println("NO idea!");
            } else
                System.out.println("Dont know what you're talking about....");
        } else {
            System.out.println("No idea what you're talking about...");
        }
    }

    double calc_credit(String space_name, String coin_name) throws Exception {
        int coin_count = break_string(space_name);
        //Find the corresponding coin name :
        double credvalue = coincredit.get(coin_name);
        return credvalue * coin_count;
    }

    void assign_credits(String space_name, String coin_name, int cred_count) throws Exception {
        int coin_count_here = break_string(space_name);
        double single_coin_credit_val = (double) cred_count / coin_count_here;
        coincredit.put(coin_name, single_coin_credit_val);

    }

    void add_alias(String new_alias, char real_name) throws Exception {
        aliasnames.put(new_alias, real_name);
    }

    int break_string(String input) throws Exception {
        String[] pieces = input.split("\\s");
        String converted = new String();
        for (int i = 0; i < pieces.length; i++) {
            converted = converted + search_alias(pieces[i]);
        }
        int t = convert(converted);
        return t;
    }

    char search_alias(String resolve_alias) {
        return aliasnames.get(resolve_alias);
    }

    int convert(String source) throws Exception {
        int curr;
        int sum = 0;
        int prev = 10000000;
        int count;
        char check;
        try {
            if (!source.matches("[IVXLCDM]+")) {
                throw new Exception("Should only contain roman-numerals");
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            System.exit(0);
        }
        count = 1;
        for (int i = 0; i < source.length(); i++) {
            curr = space2roman.get(source.charAt(i));
            try {

                if (prev == curr) {
                    check = source.charAt(i);
                    if (check == 'D' || check == 'V' || check == 'L') // D,V,L can never be repeated...
                        throw new Exception("D,V and L can never be repeated");
                    else // I,X,C,M can be repeated up to 3 times
                    {
                        count++;
                        if (count > 3)
                            throw new Exception("I,X,C and M can never be repeated more than 3 times");
                    }
                } else
                    count = 1;
                if (prev < curr) {
                    //Add restrictions here before doing the subtraction
                    // I can only be subtracted from V and X
                    // X can be only subtracted from L and C
                    // C can be only subtracted from D and M
                    // V, L and D can never be subtracted.
                    // pass the source string, as well as current index
                    check_constraint('I', 'V', 'X', source, i - 1);
                    check_constraint('X', 'L', 'C', source, i - 1);
                    check_constraint('C', 'D', 'M', source, i - 1);
                    if (source.charAt(i - 1) == 'L' || source.charAt(i - 1) == 'V' || source.charAt(i - 1) == 'D')
                        throw new Exception("Roman numeral construction violated");
                    sum = sum - 2 * space2roman.get(source.charAt(i - 1)) + space2roman.get(source.charAt(i));
                } else
                    sum = sum + space2roman.get(source.charAt(i));
            } catch (Exception e) {
                System.out.println(e.getMessage());
                System.exit(0);
            }
            prev = curr;
        }
        return sum;
    }

    void check_constraint(char sub, char check1, char check2, String source, int index) throws Exception {
        if (source.charAt(index) == sub)
            if (!(source.charAt(index + 1) == check1 || source.charAt(index + 1) == check2))
                throw new Exception("Roman numeral construction rules violated");
    }

}

